#include <iostream>
#include "TestGenerator.h"
#include "Trace.h"
#include "Generator.h"
#include <assert.h>

using namespace tsc::common;
using namespace std;

namespace tsc_test
{

void
TestState1::handleEvent(tsc::common::Generator* generator, tsc::common::EventData*& event)
{
	INFO("handle event=0x%x in state: %s, genId=%u",
			event->sigNo(), getName(), generator->getGeneratorId());

	if(event->sigNo() == SIGNAL_BASE + 3)
	{
		INFO("Received 3-rd event changing state to state2");
		generator->changeState(TestStateMap::instance()._state2);
	}

	EventData::deleteEvent(event);
}

void
TestState2::handleEvent(tsc::common::Generator* generator, tsc::common::EventData*& event)
{
	INFO("handle event=0x%x in state: %s, genId=%u",
			event->sigNo(), getName(), generator->getGeneratorId());

	if(event->sigNo() == SIGNAL_BASE + 0x4)
	{
		INFO("Creating bonus event");
		EventData* newEvent = new TestEvent(0xdd);
		generator->injectEvent(newEvent);
	}

	EventData::deleteEvent(event);
}

TestGenerator::TestGenerator(GeneratorEnvironment& ge) :
	Generator(ge, TestStateMap::instance().initialState())
{
	INFO("Created TestGenerator");
}

}

int main(int argc, char* argv[])
{
  TRACE_PROCESS("tsc_test");
  ENABLE_TRACE(ENTER);
  ENABLE_TRACE(RETURN);
  ENABLE_TRACE(TRACE7);
  ENABLE_TRACE(STATE_CHANGE);

  GeneratorEnvironment ge;

  Generator* g = new tsc_test::TestGenerator(ge);
  g->setGeneratorId(1234);

  for(size_t i = 0; i < 8; ++i)
  {
	  EventData* e = new tsc_test::TestEvent(i);

	  g->injectEvent(e);
	  assert(e == 0);
  }

  g->removeGenerator();
  ge.deleteRemovedGenerators();

  return 0;
}
